package com.icss.ah.applyHomeDisplay.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.j2ee.util.UUID;

/**
 * 申请首页展示服务类
 * 1、首页列表查询
 * 2、新增、修改首页展示内容
 * 3、展示内容详情
 * @date 2017-8-25
 */
@Service
@Transactional
public class ApplyHomeDisplayService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I"); 
	
	private EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
	
	/**
	 * 首页列表查询
	 */
	public EasyuiDataGridJson queryApplyHomeDisplayList(Map<String, String> map){
		try {
			List<Map<String, Object>> list = sqlSessionTemplate.selectList("ApplyHomeDisplayBean.queryApplyHomeDisplayList",map);
			Long total = sqlSessionTemplate.selectOne("ApplyHomeDisplayBean.queryApplyHomeDisplayListCount",map);
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
			infolog.info("ApplyHomeDisplayService|queryApplyHomeDisplayList|list:"+list);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ApplyHomeDisplayService|queryApplyHomeDisplayList", e);
		}
		return dataGridJson;
	}
	
	/**
	 * 新增、修改首页展示内容
	 */
	public String save(Map<String, String> map){
		String result = "保存失败";
		try {
			String type = map.get("type");
			if("add".equals(type)){//新增
				map.put("uuid", new UUID().toString());
				infolog.info("ApplyHomeDisplayService|saveInfo|para:"+map);
				sqlSessionTemplate.insert("ApplyHomeDisplayBean.saveInfo",map);
			}else{//编辑
				infolog.info("ApplyHomeDisplayService|updateInfo|para:"+map);
				sqlSessionTemplate.update("ApplyHomeDisplayBean.updateInfo",map);
			}
			result = "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ApplyHomeDisplayService|save", e);
		}
		return result;
	}
	
	/**
	 * 展示内容详情
	 */
	public Map<String, Object> getApplyHomeDisplayInfo(String uuid){
		Map<String, Object> bean = new HashMap<String, Object>();
		try {
			bean = sqlSessionTemplate.selectOne("ApplyHomeDisplayBean.getApplyHomeDisplayInfo",uuid);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ApplyHomeDisplayService|getApplyHomeDisplayInfo", e);
		}
		return bean;
	}

}
